<?php
/**
 * Hols class Image_Blog
 *
 * @author daantimmer
 *
 * @package Library
 * @subpackage Image
 */

/**
 * This class represents a blob of colour found in an image
 *
 * @author daantimmer
 */
class Image_Blob{
	
	/**
	 * @var integer
	 */
	private $index;

	/**
	 * @var integer
	 */
	private $start	= array();
	
	/**
	 * @var integer
	 */
	private $end	= array();

	/**
	 * @var Image_Lib_Color
	 */
	private $color;

	/**
	 * Constructor for Image_Blog
	 *
	 * @param integer $start	top left x-coordinate
	 * @param integer $end		top	right x-coordinate
	 * @param integer $y		height
	 * @param integer $index	Image_ColorIndex index
	 * @param Image_Lib_Color $color
	 * @return void
	 */
	public function __construct($start,$end, $y,$index,Image_Lib_Color $color){
		$this->start[0] = $start;
		$this->end[0] = $end;

		$this->start[1] = $this->end[1] = $y;

		$this->color = $color;
		$this->index = $index;
	}

	/**
	 * Returns top left x-coordinate
	 *
	 * @return integer
	 */
	public function getXStart(){
		return $this->start[0];
	}

	/**
	 * Returns top left y-coordinate
	 *
	 * @return integer
	 */
	public function getYStart(){
		return $this->start[1];
	}

	/**
	 * Returns bottom right x-coordinate
	 *
	 * @return integer
	 */
	public function getXEnd(){
		return $this->end[0];
	}

	/**
	 * Returns bottom right y-coordinate
	 *
	 * @return integer
	 */
	public function getYEnd(){
		return $this->end[1];
	}

	/**
	 * Sets top left x-coordinate
	 *
	 * @param integer $x
	 * @return void
	 */
	public function setXStart($x){
		$this->start[0] = $x;
	}

	/**
	 * Sets top left y-coordinate
	 *
	 * @param integer $y
	 * @return void
	 */
	public function setYStart($y){
		$this->start[1] = $y;
	}

	/**
	 * Sets bottom right x-coordinate
	 *
	 * @param integer $x
	 * @return void
	 */
	public function setXEnd($x){
		$this->end[0] = $x;
	}

	/**
	 * sets bottom right y-coordinate
	 *
	 * @param integer $y
	 * @return void
	 */
	public function setYEnd($y){
		$this->end[1] = $y;
	}

	/**
	 * Returns index specified by Image_ColorIndex enum
	 *
	 * @see Image_ColorIndex
	 * @return integer Image_ColorIndex index
	 */
	public function getIndex(){
		return $this->index;
	}

	/**
	 * Returns the displayed color
	 *
	 * @return Image_Lib_Color
	 */
	public function getColor(){
		return $this->color;
	}

	/**
	 * Returns true if $start,$end and $y result in touching the innerbody or side of this blob
	 *
	 * @param integer $start
	 * @param integer $end
	 * @param integer $y
	 * @return boolean
	 */
	public function touch($start,$end,$y){
		if($y > $this->getYEnd()+1) return false;

		if(		($start	>= $this->getXStart() && $start <= $this->getXEnd())
		||	($end	>= $this->getXStart() && $end	<= $this->getXEnd())
		||	($start	<= $this->getXStart() && $end	>= $this->getXEnd()))
		return true;

		return false;
	}

	/**
	 * Returns true if $blob touches this blob
	 *
	 * @param Image_Blob $blob
	 * @return boolean
	 */
	public function touchBlob(Image_Blob $blob){
		if(		(Image_Lib_Util::equalBetween($this->getXStart(), $blob->getXStart(),	$this->getXEnd())
		&&	Image_Lib_Util::equalBetween($this->getYStart(), $blob->getYStart(),	$this->getYEnd()))
			
		//Check if top right is within our rectangle
		||	(Image_Lib_Util::equalBetween($this->getXStart(), $blob->getXEnd(), 	$this->getXEnd())
		&&	Image_Lib_Util::equalBetween($this->getYStart(), $blob->getYStart(),	$this->getYEnd()))
			
		//Check if bottom left is within our rectangle
		||	(Image_Lib_Util::equalBetween($this->getXStart(), $blob->getXStart(),	$this->getXEnd())
		&&	Image_Lib_Util::equalBetween($this->getYStart(), $blob->getYEnd(),		$this->getYEnd()))
			
		//Check if bottom right is within our rectangle
		||	(Image_Lib_Util::equalBetween($this->getXStart(), $blob->getXEnd(),		$this->getXEnd())
		&&	Image_Lib_Util::equalBetween($this->getYStart(), $blob->getYEnd(),		$this->getYEnd()))

		//Check if our top left is within blob's rectangle
		||	(Image_Lib_Util::equalBetween($blob->getXStart(), $this->getXStart(),	$blob->getXEnd())
		&&	Image_Lib_Util::equalBetween($blob->getYStart(), $this->getYStart(),	$blob->getYEnd()))
			
		//Check if our top right is within blob's rectangle
		||	(Image_Lib_Util::equalBetween($blob->getXStart(), $this->getXEnd(), 	$blob->getXEnd())
		&&	Image_Lib_Util::equalBetween($blob->getYStart(), $this->getYStart(),	$blob->getYEnd()))
			
		//Check if our bottom left is within blob's rectangle
		||	(Image_Lib_Util::equalBetween($blob->getXStart(), $this->getXStart(),	$blob->getXEnd())
		&&	Image_Lib_Util::equalBetween($blob->getYStart(), $this->getYEnd(),		$blob->getYEnd()))
			
		//Check if our bottom right is within blob's rectangle
		||	(Image_Lib_Util::equalBetween($blob->getXStart(), $this->getXEnd(),		$blob->getXEnd())
		&&	Image_Lib_Util::equalBetween($blob->getYStart(),	$this->getYEnd(),		$blob->getYEnd())))
		return true;
		return false;
	}

	/**
	 * Sets this blob to match the max-size of either this blob or $blob
	 *
	 * @param Image_Blob $blob
	 * @return void
	 */
	public function expand(Image_Blob $blob){
		$this->setXStart(min($blob->getXStart(), $this->getXStart()));
		$this->setYStart(min($blob->getYStart(), $this->getYStart()));

		$this->setXEnd(max($blob->getXEnd(), $this->getXEnd()));
		$this->setYEnd(max($blob->getYEnd(), $this->getYEnd()));
	}

	/**
	 * Returns coordinates in the form of {{xstart,ystart},{xend,yend}}
	 *
	 * @return array integer{{xs,ys},{xe,ye}}
	 */
	public function get(){
		return array(array($this->start[0],$this->start[1]),array($this->end[0],$this->end[1]));
	}


}